from seleniumwire import webdriver

from common.core.api.webdriver_api import *
from common.core.meta import ApiAliasMeta
from common.core.meta import Xpath
from common.core.page.mixins import ElementsLookUpMixin
from common.core.page.webdriver import ChromeOptions
from common.core.api.element_api import ElementActionProxy
from common.core.globals import PageObject


class BasePage(PageObject, ElementsLookUpMixin, metaclass=ApiAliasMeta):

    def __init__(self, driver=None, debug=False):
        self._debug = debug
        self._webdriver = driver or webdriver.Chrome(options=ChromeOptions())
        self._webdriver.maximize_window()
        ElementsLookUpMixin.__init__(self)
        p.set_proxy(self)

    @property
    def debug(self):
        return self._debug

    @property
    def webdriver(self) -> webdriver.Chrome:
        return self._webdriver

    # 支持对象索引方式查找定位器
    def __getitem__(self, locator: Union[str, Xpath]):
        return self.find(locator)

    get = Get("地址跳转")
    redirect = Redirect("页面跳转")
    quit = DriverQuit("退出")
    find = ElementActionProxy("元素查找")
    scroll_to = ScrollTo("窗口滚动")  # 页面滚劥到指定位置，顶部 (0, 0)， 最底部 (0, document.body.scrollHeight)
    add_screen = AddScreen("添加截屏")
    catch_request = CatchRequest("请求步骤")
    switch_handler = SwitchHandler("窗口跳转")
    execute_script = ExecuteScript("执行脚本")
    action_chains = UserActionChains("动作链")
    switch_to_frame = SwitchToFrame("框架跳转")
    switch_to_parent = SwitchToParent("跳回父框架")
